<template>
  <view class="bank-statement">
    <!-- 输入区域 -->
    <view class="input-section">
      <uni-forms :modelValue="formData">
        <uni-forms-item label="月薪金额" name="salary">
          <uni-easyinput 
            v-model="formData.salary" 
            type="digit" 
            placeholder="输入税前月薪"
            suffixIcon="currency"
          />
        </uni-forms-item>
        <uni-forms-item label="发放月数" name="months">
          <uni-number-box 
            v-model="formData.months" 
            :min="1" 
            :max="24"
          />
        </uni-forms-item>
      </uni-forms>
      <button @click="generateStatement" class="generate-btn">生成银行流水</button>
    </view>

    <!-- 银行流水列表 -->
    <view class="statement-list" v-if="statements.length > 0">
      <view class="statement-header">
        <text>工资发放流水（{{ statements[0].account }}）</text>
        <text>合计: {{ totalAmount }}元</text>
      </view>

      <view 
        v-for="(item, index) in statements" 
        :key="index" 
        class="statement-item"
        :class="{ 'income': item.type === '工资入账' }"
      >
        <view class="item-left">
          <text class="transaction-date">{{ item.date }}</text>
          <text class="transaction-type">{{ item.type }}</text>
          <text class="transaction-id">流水号: {{ item.id }}</text>
        </view>
        <view class="item-right">
          <text class="amount">{{ item.amount }}元</text>
          <text class="balance">余额: {{ item.balance }}元</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue';

const formData = ref({
  salary: 20000,
  months: 12
});

const statements = ref([]);
const totalAmount = ref(0);

// 生成银行流水
const generateStatement = () => {
  statements.value = [];
  totalAmount.value = formData.value.salary * formData.value.months;
  
  let balance = 0;
  const accountNumber = '622588******' + Math.floor(Math.random() * 10000).toString().padStart(4, '0');
  
  for (let i = 0; i < formData.value.months; i++) {
    const currentDate = new Date();
    currentDate.setMonth(currentDate.getMonth() + i);
    
    // 计算税后工资（简化版）
    const tax = calculateTax(formData.value.salary);
    const netSalary = formData.value.salary - tax;
    balance += netSalary;
    
    statements.value.push({
      id: generateTransactionId(),
      date: formatBankDate(currentDate),
      type: '工资入账',
      amount: netSalary.toFixed(2),
      balance: balance.toFixed(2),
      account: accountNumber,
      remark: `${i+1}月工资`
    });
  }
};

// 生成交易流水号
const generateTransactionId = () => {
  const datePart = new Date().toISOString().replace(/-|T|:/g, '').slice(0, 14);
  return datePart + Math.floor(Math.random() * 1000).toString().padStart(3, '0');
};

// 银行日期格式
const formatBankDate = (date) => {
  return `${date.getFullYear()}/${(date.getMonth()+1).toString().padStart(2, '0')}/${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
};

// 简化版个税计算
const calculateTax = (salary) => {
  const taxable = salary - 5000; // 免征额
  if (taxable <= 0) return 0;
  if (taxable <= 3000) return taxable * 0.03;
  if (taxable <= 12000) return taxable * 0.1 - 210;
  return taxable * 0.2 - 1410;
};
</script>

<style scoped>
.bank-statement {
  padding: 20rpx;
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', sans-serif;
}

.input-section {
  background: #fff;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.05);
}

.generate-btn {
  background: #3A73F7;
  color: white;
  margin-top: 30rpx;
  border-radius: 8rpx;
}

.statement-list {
  background: #fff;
  border-radius: 12rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.05);
}

.statement-header {
  padding: 24rpx 30rpx;
  background: #f5f5f5;
  display: flex;
  justify-content: space-between;
  font-size: 28rpx;
  color: #666;
  border-bottom: 1rpx solid #eee;
}

.statement-item {
  padding: 30rpx;
  display: flex;
  justify-content: space-between;
  border-bottom: 1rpx solid #f0f0f0;
}

.statement-item:last-child {
  border-bottom: none;
}

.item-left {
  flex: 1;
}

.item-right {
  text-align: right;
}

.transaction-date {
  display: block;
  font-size: 28rpx;
  color: #333;
  margin-bottom: 10rpx;
}

.transaction-type {
  display: block;
  font-size: 32rpx;
  font-weight: 500;
  margin-bottom: 6rpx;
}

.transaction-id {
  display: block;
  font-size: 24rpx;
  color: #999;
}

.amount {
  display: block;
  font-size: 34rpx;
  font-weight: 600;
  margin-bottom: 6rpx;
}

.balance {
  display: block;
  font-size: 24rpx;
  color: #999;
}

/* 收入特殊样式 */
.income .transaction-type {
  color: #3A73F7;
}
.income .amount {
  color: #09BB07;
}
</style>